﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GUI
{
    public class DragAndDrop
    {
        private Texture2D _droppedImage;

        public Texture2D DroppedImage
        {
            get { return _droppedImage; }
            set { _droppedImage = value; }
        }

        System.Windows.Forms.Form _form;

        private GraphicsDevice _GD;

        public DragAndDrop(Game game, GraphicsDevice GD)
        {
            _GD = GD;

            _form = System.Windows.Forms.Form.FromHandle(game.Window.Handle) as System.Windows.Forms.Form;

            _form.AllowDrop = true;

            _form.DragEnter += new System.Windows.Forms.DragEventHandler(form_DragEnter);
            _form.DragOver += new System.Windows.Forms.DragEventHandler(form_DragOver);
            _form.DragDrop += new System.Windows.Forms.DragEventHandler(form_DragDrop);
        }

        void form_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            // Use Data.GetDataPresent to check if the data that's being dragged either is or
            // can be converted to unicode text.
            if (e.Data.GetDataPresent(System.Windows.Forms.DataFormats.UnicodeText))
            {
                // If so, tell Windows that we will accept a copy operation for this data.
                e.Effect = System.Windows.Forms.DragDropEffects.Copy;
            }

            // Use Data.GetDataPresent to check if the data that's been dragged is a file.
            if (e.Data.GetDataPresent(System.Windows.Forms.DataFormats.FileDrop))
            {
                // If so, tell Windows that we will accept a copy operation for this data.
                // WARNING!!!!!!!!!!!!!!!!!!! Do not set this to Move if you value your files.
                // I have not tested it, but presumably your file would be "moved" into your game
                // and would disappear from your file system leaving you with no file anymore.
                // You have been warned.
                e.Effect = System.Windows.Forms.DragDropEffects.Copy;
            }

        }

        void form_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
        {
            // Use Data.GetDataPresent to check if the data that's being dragged either is or
            // can be converted to unicode text.
            if (e.Data.GetDataPresent(System.Windows.Forms.DataFormats.UnicodeText))
            {
                // If so, tell Windows that we will accept a copy operation for this data.
                e.Effect = System.Windows.Forms.DragDropEffects.Copy;
            }

            // Use Data.GetDataPresent to check if the data that's been dragged is a file.
            if (e.Data.GetDataPresent(System.Windows.Forms.DataFormats.FileDrop))
            {
                // If so, tell Windows that we will accept a copy operation for this data.
                // WARNING!!!!!!!!!!!!!!!!!!! Do not set this to Move if you value your files.
                // I have not tested it, but presumably your file would be "moved" into your game
                // and would disappear from your file system leaving you with no file anymore.
                // You have been warned.
                e.Effect = System.Windows.Forms.DragDropEffects.Copy;
            }
        }

        void form_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(System.Windows.Forms.DataFormats.FileDrop))
            {
                // DataFormats.FileDrop allows multiple files to be dropped. It returns their fully-qualified
                // paths in a string array.
                string[] files = (string[])e.Data.GetData(System.Windows.Forms.DataFormats.FileDrop);

                // Check for null just to be sure.
                if (files != null)
                {

                    try
                    {

                        if (_droppedImage != null && !_droppedImage.IsDisposed)
                        {
                            _droppedImage.Dispose();
                        }
                        _droppedImage = null;

                        using (Stream stream = File.Open(files[0], FileMode.Open))
                        {

                            _droppedImage = Texture2D.FromStream(_GD, stream);
                        }
                    }
                    catch
                    {

                    }
                }
            }
        }
    }
}
